﻿var list = [];
var draw = 0;
var draw1 = 0;
var serviceId = "";
var folderId = "";
var table1, table2;
var ServiceTypelist = GetDataServiceTypeList();
var ServiceTypeName = "";
$(function () {
    updateDataServiceFolderTree();
});

//更新服务目录select下拉框
var updateDataServiceFolderTree = function () {
    var list = GetDataServiceFolderTree();
    $("#FolderId").append(list);
}


var UpdateDataService = function (obj) {
    var id = $(obj).val();
    clearDataSource();
    if (id == "") {
        return;
    }
    console.log("正在努力的加在数据服务列表！");
    folderId = id;
    if (draw1 != 0) {
        table1.fnDraw();
    } else {
        CreateTableForDaraService();
    }
}
var clearDataSource = function () {
    if (draw != 0) {
        //table2.fnClearTable();
        $("#table-datasource").hide();
    }
}
var UpdateDataSource = function (id) {
    console.log("正在努力的加在数据源列表！");
    $("#table-datasource").show();
    serviceId = id;
    if (draw != 0) {
        table2.fnDraw();
    } else {
        CreateTableForDaraSource();
    }
}
var CreateTableForDaraService = function () {
    //初始化表格
    try {


        table1 = $('#datalist-table-dataservice').dataTable({
            "autoWidth": true,
            "bLengthChange": false,
            "oLanguage": {
                "sProcessing": "正在加载数据...",
                "sZeroRecords": "没有您要搜索的内容",
                "sInfo": "从_START_ 到 _END_ /共 _TOTAL_ 条",
                "sInfoEmpty": "记录数为0",
                "sInfoFiltered": "(全部记录数 _MAX_  条)",
                "sInfoPostFix": "",
                "sUrl": "",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "上一页",
                    "sNext": "下一页",
                    "sLast": "末页"
                }
            },
            ordering: false,
            bProcessing: true,
            bStateSave: true,
            serverSide: true,
            searching: false,
            aLengthMenu: [25],
            "ajax": {
                url: apiUrl,
                "dataSrc": function (json) {
                    if (json.IsSccuess) {
                        var newjson = new Array();
                        for (var i = 0; i < json.Data.Items.length; i++) {
                            var obj = json.Data.Items[i];
                            //var servicestr = "<a href=\"";
                            var servicetypenamestr = GetServiceTypeName(obj.TypeId);
                            var servicestr = ("<a href='javascript:void(0);'onclick='UpdateDataSource(\"" + obj.Id + "\")' >" + obj.Name + "</a>");
                            newjson.push({ "service": servicestr, "type": servicetypenamestr });
                        }
                        var tempjson = { "draw": draw1, "recordsTotal": json.Data.Total, "iTotalDisplayRecords": json.Data.Total, "data": newjson };
                        
                        return newjson;
                    } else {
                        if (IsLogined(json.ErrorCode)) {
                            alert("错误代码：" + json.ErrorCode + "，错误信息：" + json.Message);
                        }
                    }
                    return [];
                },
                "data": function (d) {
                    draw1 = d.draw;
                    return $.extend({}, { "sys:sno": "ETL" }, { "sys:act": "GetDataServiceList" }, { "draw": draw1 }, { "length": d.length }, { "FolderId": folderId }, { "Page": parseInt(d.start / d.length) });
                }
            },
            columns: [
                { data: 'service' },
                { data: 'type' }
            ]
        });
    } catch (e) {
        console.log(e);
    }

}
var CreateTableForDaraSource = function () {
    //初始化表格
    table2 = $('#datalist-table-datasource').dataTable({
        "autoWidth": true,
        "bLengthChange": false,
        "oLanguage": {
            "sProcessing": "正在加载数据...",
            "sZeroRecords": "没有您要搜索的内容",
            "sInfo": "从_START_ 到 _END_ /共 _TOTAL_ 条",
            "sInfoEmpty": "记录数为0",
            "sInfoFiltered": "(全部记录数 _MAX_  条)",
            "sInfoPostFix": "",
            "sUrl": "",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上一页",
                "sNext": "下一页",
                "sLast": "末页"
            }
        },
        ordering: false,
        bProcessing: true,
        bStateSave: true,
        serverSide: true,
        searching: false,
        aLengthMenu: [25],
        "ajax": {
            url: apiUrl,
            "dataSrc": function (json) {
                if (json.IsSccuess) {
                    var newjson = new Array();
                    for (var i = 0; i < json.Data.Items.length; i++) {
                        var datasourcestr = "<a href='javascript:void(0);' onclick=\"addData('" + serviceId + "','" + json.Data.Items[i].Id + "')\">" + json.Data.Items[i].Name + "</a>";
                        newjson.push({ "dataSource": datasourcestr, "dataTable": (json.Data.Items[i].MappingTable == "" ? "none" : json.Data.Items[i].MappingTable) });
                    }
                    var tempjson = { "draw": draw, "iTotalRecords": json.Data.Total, "iTotalDisplayRecords": json.Data.Total, "data": newjson };
                    
                    return newjson;
                } else {
                    if (IsLogined(json.ErrorCode)) {
                        alert("错误代码：" + json.ErrorCode + "，错误信息：" + json.Message);
                    }
                }
                return [];
            },
            "data": function (d) {
                draw = d.draw;
                return $.extend({}, { "sys:sno": "ETL" }, { "sys:act": "GetDataSourceList" }, { "HasTask": "A" }, { "draw": d.draw }, { "length": d.length }, { "DataServiceId": serviceId }, { "Page": parseInt(d.start / d.length) });
            }
        },
        columns: [
            { data: 'dataSource' },
            { data: 'dataTable' }


        ],
        fnDrawCallback: function () {
        }
    });
}
var GetServiceTypeName = function (typeId) {
    if (ServiceTypelist != null && ServiceTypelist.length != 0) {
        for (var i = 0; i < ServiceTypelist.length; i++) {
            if (typeId == ServiceTypelist[i].Id) {
                return ServiceTypelist[i].Caption;
            }
        }
    }
    return "暂无";
}
//获取服务获取api的Sys_Sno
var GetSys_Sno = function (typeName) {
    if (typeName == "WebService") {
        return "WS";
    } else if (typeName == "Excel") {
        return "Excel";
    } else if (typeName == "SQLServer") {
        return "SQL";
    } else {
        return "";
    }

}
var addData = function (id, sourceId) {
    var url = "SchedulingTaskCreateTwo.html?Id=" + id + "&dataSourceId=" + sourceId;
    window.location.href = url;

}
